﻿namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using FrequencyDomain;

    /// <summary>
    /// edge detection class
    /// </summary>
    public class EdgeDetection
    {
        /// <summary>
        /// static object of this class to use this without create object
        /// </summary>
        private static EdgeDetection edgeDetection = new EdgeDetection();

        /// <summary>
        /// mask will able it in buffer of image
        /// </summary>
       private Mask mask = new Mask();

       /// <summary>
       /// my detection type
       /// </summary>
       private LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType myDetection;

       /// <summary>
       /// Gets or sets the edge detection1.
       /// </summary>
       /// <value>
       /// The edge detection1.
       /// </value>
       public static EdgeDetection EdgeDetection1
       {
           get { return EdgeDetection.edgeDetection; }
           set { EdgeDetection.edgeDetection = value; }
       }

       /// <summary>
       /// Gets or sets my detection.
       /// </summary>
       /// <value>
       /// My detection.
       /// </value>
       public LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType MyDetection
       {
           get { return this.myDetection; }
           set { this.myDetection = value; }
       }

       /// <summary>
       /// Makes the edge detection.
       /// </summary>
       /// <param name="image">The image.</param>
       /// <param name="myDetection">My detection type.</param>
       /// <returns>
       /// the new image
       /// </returns>
       public Bitmap MakeEdgeDetection(Bitmap image, Enum_needed.DetectionAndSharpingType myDetection)
        {
            Color[,] bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
            this.mask.MaskSize = 3;
            Color[,] bufferWithPadding = LinearConvolution.LinerConvalution.AddBorder(bufferOfImage, 3);
            switch (myDetection)
            {
                case LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType.Point:
                    this.mask.MaskValues = new double[,] { { -1, -1, -1 }, { -1, 8, -1 }, { -1, -1, -1 } };
                    break;
                case LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType.Horizontal:
                    this.mask.MaskValues = new double[,] { { 5, -3, -3 }, { 5, 0, -3 }, { 5, -3, -3 } };
                    break;
                case LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType.Vertical:
                    this.mask.MaskValues = new double[,] { { 5, 5, 5 }, { -3, 0, -3 }, { -3, -3, -3 } };
                    break;
                case LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType.RightDiagonal:
                    this.mask.MaskValues = new double[,] { { 5, 5, -3 }, { 5, 0, -3 }, { -3, -3, -3 } };
                    break;
                case LastVersionOfIpPackage.Enum_needed.DetectionAndSharpingType.LeftDiagonal:
                   this.mask.MaskValues = new double[,] { { -3, 5, 5 }, { -3, 0, 5 }, { -3, -3, -3 } };
                    break;
                default:
                    break;
            }

            try
            {
                return LinearConvolution.LinerConvalution.LinearConvolutionFn(bufferOfImage, bufferWithPadding, this.mask, Enum_needed.PostProcesses.Normalization);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }
        }
    }
}
